
import { Button, Form, Input, Select, Spin } from 'antd';
import React,{useState,useEffect} from 'react';
import { roleAdd,roleGet,userReg } from '../../api/user';
const {Option} = Select
const layout = {
  labelCol: {
    span: 4,
  },
  wrapperCol: {
    span: 19,
  },
};
const tailLayout = {
  wrapperCol: {
    offset: 8,
    span: 16,
  },
};

const UserManger = () => {
  const [form] = Form.useForm();
  let [loading,setLoading] = useState(false)
  let [roleList,setRoleList] = useState([])

  useEffect(()=>{
    roleGet().then(res=>{
      setRoleList(res.data.results)
    })
  },[])

  const onFinish = (values) => {
    console.log(values);
    setLoading(true) //开启loading
    userReg(values).then(res=>{
      setLoading(false) //关闭loading
    }).catch(err=>{
      setLoading(false)
    })
  };

  const onReset = () => {
    form.resetFields();
  };

  return (
    <Spin spinning={loading}>
      <Form {...layout} form={form} name="control-hooks" onFinish={onFinish}>
        <Form.Item
          name="username"
          label="账号"
          rules={[
            {
              required: true,
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="password"
          label="密码"
          rules={[
            {
              required: true,
            },
          ]}
        >
          <Input type='password'/>
        </Form.Item>
        <Form.Item
          name="rolecode"
          label="角色"
          rules={[
            {
              required: true,
            },
          ]}
        >
          <Select>
            {
              roleList.map(item=>{
                return <Option key={item.objectId} value={item.rolecode}>
                  {item.rolename}
                </Option>
              })
            }
          </Select>
        </Form.Item>
        <Form.Item {...tailLayout}>
          <Button type="primary" htmlType="submit">
            提交
          </Button>
          <Button htmlType="button" onClick={onReset}>
            重置
          </Button>
        </Form.Item>
      </Form>
    </Spin>
  );
};

export default UserManger;